/**
 * vant框架相关的工具方法
 */

import {
    allowMultipleToast,
    showConfirmDialog,
    showDialog,
    showNotify,
    showToast
} from "vant";

import type {
    ToastWrapperInstance
} from 'vant'


// --------------------------- 消息方法，每个项目几乎必须 ----------------------------
allowMultipleToast();
/**轻提示
 * @param message
 * @param option
 * @param [option.type] 提示类型。默认：text
 * @param [option.position] 位置。默认：bottom
 * @param [option.icon] 自定义图标，支持传入图标名称或图片链接。默认：
 * @param [option.overlay] 是否显示背景遮罩层。默认：false
 * @param [option.duration] 展示时长(ms)，值为 0 时，toast 不会消失。默认：2000
 */
function toast(message:string, option?:{
    type?: 'success'|'fail'|'html'|'text';
    position?: 'top'|'bottom'|'middle';
    icon?: string;
    overlay?: boolean;
    duration?: number;
}) {
    return showToast({
        message,
        position: option?.position || 'bottom',
        type: option?.type,
        icon: option?.icon,
        overlay: option?.overlay || false,
        duration: option?.duration,
    });
}
toast.ok = toast.success = function (message:string){
    return toast(message, {type: 'success'});
}
toast.fail = toast.error = function (message:string){
    return toast(message, {type: 'fail'});
}
const msg =  toast

let loadingWrapper:ToastWrapperInstance|undefined;
/**loading弹窗，全局单例
 * @param message 提示文本。默认：加载中...
 * @param option
 * @param [option.show] 是否显示弹窗。默认：当前状态取反
 * @param [option.icon] 自定义图标，支持传入图标名称或图片链接。默认：
 * @param [option.overlay] 是否显示背景遮罩层。默认：false
 */
function loading(message?:string, option?: {
    show?: boolean;
    icon?: string;
    overlay?: boolean;
}) {
    let show = !loadingWrapper;
    if (option && typeof option.show !== 'undefined') show = option.show;
    if (show){
        if (loadingWrapper){
            if (message) loadingWrapper.message = message;
        }else {
            loadingWrapper = showToast({
                type: 'loading',
                message: message || '加载中...',
                forbidClick: false,
                duration: 0,
                icon: option?.icon,
                overlay: option?.overlay,
            });
        }
    }else {
        if (loadingWrapper){
            loadingWrapper.close();
        }
        loadingWrapper = undefined;
    }
}
loading.show = function (message?:string){
    loading(message, {show:true});
}
loading.hide = function (message?:string){
    loading(message, {show:false});
}


/**
 * 提示弹窗，返回 Promise 。点击确认后，Promise兑现
 * @param message
 * @param option
 * @param option.confirmButtonText 确认按钮文本
 * @param option.title 窗口标题
 */
function alert(message:string, option?:{
    title?:string;
    confirmButtonText?: string;
}){
    let title = option?.title || '提示';
    return showDialog({
        title: title,
        message: message,
        confirmButtonText: option?.confirmButtonText || '确认',
    });
}

/**
 * 提示弹窗，返回 Promise 。点击确认后，Promise兑现；点击取消后，Promise拒绝
 * @param message
 * @param option
 * @param option.confirmButtonText 确认按钮文本
 * @param option.cancelButtonText 取消按钮文本
 * @param option.title 窗口标题
 */
function confirm(message:string, option?:{
    title?:string;
    confirmButtonText?: string;
    cancelButtonText?: string;
}){
    let title = option?.title || '确认操作';
    return showConfirmDialog({
        title: title,
        message: message,
        confirmButtonText: option?.confirmButtonText || '确认',
        cancelButtonText: option?.cancelButtonText || '取消',
    });
}

/**通知消息，返回 Promise 。点击通知后，Promise兑现
 * @param message
 * @param option
 * @param [option.type] 提示类型。默认：primary
 * @param [option.position] 位置。默认：bottom
 * @param [option.icon] 自定义图标，支持传入图标名称或图片链接。默认：
 * @param [option.overlay] 是否显示背景遮罩层。默认：false
 * @param [option.duration] 展示时长(ms)，值为 0 时，toast 不会消失。默认：2000
 */
function notification(message:string, option?:{
    type?: 'primary'|'success'|'warning'|'danger';
    duration?: number;
    onClick?: (e:MouseEvent)=>{}
}){
    return new Promise((resolve)=>{
        showNotify({
            message: message,
            type: option?.type || 'primary',
            duration: option?.duration,
            onClick: (e:MouseEvent)=>{
                resolve(e);
                if (typeof option?.onClick === 'function') option.onClick(e);
            }
        });
    })
}
notification.ok = notification.success = function (message:string){
    return notification(message, {type:'success'});
}
notification.fail = notification.error = function (message:string){
    return notification(message, {type:'danger'});
}
notification.warning = function (message:string){
    return notification(message, {type:'warning'});
}

// ------------------------------- 消息类方法end ---------------------------------

export {msg, toast, loading, alert, confirm, notification}